from django.db import models
from apps.resp.response import USER_NOT_EXIT_ERROR
from passlib.hash import pbkdf2_sha256
from apps.commonapp.models import School,Company
# Create your models here.
class User(models.Model):
    username = models.CharField(max_length=200,blank=False,unique=True)
    password = models.CharField(max_length=200,blank=False,null=False)
    is_valid = models.BooleanField(default=False)

    @classmethod
    def verify(cls,username,passwd):
        users = cls.objects.filter(username=username)
        if len(users) == 0:
            raise USER_NOT_EXIT_ERROR
        is_valid = pbkdf2_sha256.verify(passwd, users[0].password)
        return is_valid
    @classmethod
    def pwd(cls,passwd):
        v_key = pbkdf2_sha256.hash(passwd)
        return v_key
    def __str__(self) -> str:
        return "用户模型：{}".format(self.username)
    
class Uservalid(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    school = models.ForeignKey(School,on_delete=models.SET_NULL,null=True,blank=True,default=None)
    company = models.ForeignKey(Company,on_delete=models.SET_NULL,null=True,blank=True,default=None)
    company_id_card = models.CharField(max_length=200,null=True,blank=True,help_text="公司员工编号")
    school_id_card = models.CharField(max_length=200,null=True,blank=True,help_text="学校学号")
    user_id_card = models.CharField(max_length=200,null=True,blank=True,help_text="用户身份证号码")
    boss_phone = models.CharField(max_length=200,null=True,blank=True,help_text="直属领导联系号码")

    is_active = models.BooleanField(default=False)
    def __str__(self) -> str:
        return "用户验证模型"